<?php
namespace Common\Library;
/**
 * ---------------------------------------------------------------------------------------------------------------
 * 图片验证码类
 * ---------------------------------------------------------------------------------------------------------------
 * @author		Hismer <odaytudio@gmail.com>
 * @since		2014-02-27
 * @copyright	Copyright (c) 2014-2014 Swiftx Inc.
 * ---------------------------------------------------------------------------------------------------------------
 * @property String Code
 * ---------------------------------------------------------------------------------------------------------------------
 */
class Verify{

    private $_width;		// 验证码图片宽度
    private $_height;		// 验证码图片高度
    private $_codeNum;		// 验证码字符个数
    private $_code;		// 验证码字符
    private $_image;		// 验证码画布

    /**
     * 构造函数
     * @param int|验证码宽度 $width 验证码宽度
     * @param int|验证码高度 $height 验证码高度
     * @param int|验证码所包含的字符格个数 $codeNum 验证码所包含的字符格个数
     */
    public function __construct($width=80,$height=20,$codeNum=4){
        $this->_width = $width;
        $this->_height = $height;
        $this->_codeNum = $codeNum;
        $this->_code = $this->_createCheckCode();
    }

    /**
     * 魔术方法GET/SET
     */
    public function __get($name){
        $method = '_get'.$name;
        return $this->$method();
    }

    private function _getWidth(){
        return $this->_width;
    }

    private function _getHeight(){
        return $this->_height;
    }

    private function _getCodeNum(){
        return $this->_codeNum;
    }

    private function _getCode(){
        return $this->_code;
    }

    /**
     * 显示图像
     */
    public function showImage(){
        $this->_getcreateImage();
        $this->_outputText();
        $this->_setDisturbColor();
        $this->_outputImage();
    }

    /**
     * 创建图像
     */
    private function _getCreateImage(){
        $this->_image = imagecreatetruecolor($this->_width, $this->_height);
        $back = imagecolorallocate($this->_image, 255, 255, 255);
        $border = imagecolorallocate($this->_image, 255, 255, 255);
        imagefilledrectangle($this->_image, 0, 0, $this->_width-1 ,$this->_height-1, $border);
    }

    /**
     * 往画布上写入文字
     */
    private function _outputText(){
        for($i=0; $i<$this->_codeNum; $i++) {
            $bg_color = imagecolorallocate($this->_image, rand(0,255), rand(0,128), rand(0,255));
            $x = floor($this->_width/$this->_codeNum)*$i+3;
            $y = rand(0, $this->_height-15);
            imagechar($this->_image, 5, $x, $y, $this->_code[$i], $bg_color);
        }
    }

    /**
     * 干扰码设置
     */
    private function _setDisturbColor(){
        for($i=0; $i<=100; $i++){
            $color = imagecolorallocate($this->_image, rand(0,255), rand(0,255), rand(0,255));
            imagesetpixel($this->_image, rand(1,$this->_width-2), rand(1,$this->_height-2),$color);
        }
        imagesetpixel($this->_image, rand(1, $this->_width-2), rand(1,$this->_height-2), $color);
    }

    /**
     * 输出图像
     */
    private function _outputImage(){
        ob_start();
        if(imagetypes()&IMG_GIF){
            header("Content_type:image/gif");
            imagegif($this->_image);
        }
        else if(imagetypes()&IMG_JPG){
            header("Content-type:image/jpeg");
            imagejpeg($this->_image,"",0.5);
        }
        else if(imagetypes()&IMG_PNG){
            header("Content-type:image/png");
            imagejpeg($this->_image);
        }
        else if(imagetypes()&IMG_WBMP){
            header("Content-type:image/vnd.wap.wbmp");
            imagejpeg($this->_image);
        }
        else {
            die("PHP不支持图像创建");
        }
    }

    /**
     * 创建随机验证字符
     */
    private function _createCheckCode(){
        $asc_number = '';
        for($i=0; $i<$this->_codeNum; $i++){
            $number = rand(0, 2);
            switch($number){
                case 0: $rand_number = rand(48,57); break;		// 数字
                case 1: $rand_number = rand(65,90); break;		// 大写字母
                case 2: $rand_number = rand(97,122); break;		// 小写字母
            }
            $asc_number .= sprintf("%c", $rand_number);
        }
        return $asc_number;
    }

    /**
     * 析构函数，销毁图像资源
     */
    public function __destruct(){
        if(!empty($this->_image)){
            imagedestroy($this->_image);
        }
    }
}